package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.odin.solution.enumcode.statistics.UserActionBusinessTypeEnum;
import com.homedo.odin.solution.model.request.mng.useractionlog.UserActionLogListRequestBean;
import com.homedo.odin.solution.model.request.mng.useractionlog.UserActionStatisticsListRequestBean;
import com.homedo.odin.solution.model.response.mng.useractionlog.UserActionLogResponseBean;
import com.homedo.odin.solution.model.response.mng.useractionlog.UserActionStatisResponseBean;
import com.homedo.odin.solution.service.UserActionLogServiceImpl;
import com.homedo.odin.solution.service.UserActionStatisServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenlei
 */
@Api(description = "用户行为统计", tags = {"UserActionLog"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/statistics")
public class MngUserActionStatisticsController extends BaseController<UserActionStatisServiceImpl> {

    /**
     * 业务类型，要与枚举UserActionBusinessTypeEnum同步
     */
    public final static String BIZ_TYPE = "advertising_show, advertising_click, contact_us_show, hot_keyword_click, common_type_click, brand_click, hot_solution_click, best_solution_click";

    @Autowired
    private UserActionLogServiceImpl userActionLogService;

    @ApiOperation(value = "OMS_获取某分类用户行为统计列表", response = UserActionStatisResponseBean.class, httpMethod = "GET")
    @GetMapping("/{bizType}")
    public ResultData getLogsStatistics(@ApiParam(value = "业务类型", required = true, allowableValues = BIZ_TYPE)
    @PathVariable(value = "bizType") String bizType,
            @Valid @ModelAttribute UserActionStatisticsListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultData
                    .newInstance(new Meta(Meta.CODE_451.getCode(), bindingResult.getFieldError().getDefaultMessage()));
        }
        if (!UserActionBusinessTypeEnum.dbValueExist(bizType)) {
            return ResultData.newInstance(Meta.CODE_451, "业务类型参数有误！");
        }

        return ResultData.newInstance(baseService.getStatisByType(requestBean, bizType));
    }

    @ApiOperation(value = "OMS_获取用户行为日志列表", response = UserActionLogResponseBean.class, httpMethod = "GET")
    @GetMapping("/logs/{bizType}")
    public ResultData getActionLogs(@ApiParam(value = "业务类型", required = true, allowableValues = BIZ_TYPE)
    @PathVariable(value = "bizType") String bizType,
            @Valid @ModelAttribute UserActionLogListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultData
                    .newInstance(new Meta(Meta.CODE_451.getCode(), bindingResult.getFieldError().getDefaultMessage()));
        }
        if (!UserActionBusinessTypeEnum.dbValueExist(bizType)) {
            return ResultData.newInstance(Meta.CODE_451, "业务类型参数有误！");
        }

        return ResultData.newInstance(userActionLogService.getLogs(requestBean, bizType));
    }
}
